<?php
namespace Home\Controller;
use Think\Controller;
class LightController extends CommonController {
	public function index(){
		$id = I('id');
        if (!$id) {
            $this->error('参数错误');
        }
		$data = M('device a')
    		->where(array('a.id'=>$id))
    		->field('a.name,a.id,a.shop_id,b.status,b.cate_name,b.pest,a.code_num,a.cate_id')
    		->join("left join zm_device_cate b on a.cate_id=b.id")
    		->find();
        if(!$data){
            $this->error('参数错误');
        }
        
        $shop_name = M('shop')->where(array('id' => $data['shop_id']))->getField('shop_name');
        $this->assign('shop_name',$shop_name);
    	$status = M('device_status')
    		->where('cate_id='.$data['cate_id'])
    		->field('status_name,id,is_next')
    		->select();

    	$pest_id = explode(',', $data['pest']);
    	$pest_name = M('animal')
    		->where(array('id'=>array('in',$pest_id)))
    		->field('name,id')
    		->select();
    	$this->assign('data',$data);
    	$this->assign('status',$status);
        $this->assign('pest_name',$pest_name);
    	$this->display();
	}
    
    public function status_2(){
        $id = I('id');
        $two_status = M('device_status_next')
                    ->where(array('st_id'=>$id,'fu_id'=>0))
                    ->field('id,name,is_next')
                    ->select();
        $this->assign('two_status',$two_status);
        $this->assign('id',$id);
        if(3 == $id){
            $this->display('status_21');
        }else{
            $this->display();
        }
    }
    public function status_3(){
        $id = I('id');
        $three_status = M('device_status_next')
                        ->where(array('fu_id'=>$id))
                        ->field('id,name,is_next')
                        ->select();
        $this->assign('t_status',$three_status);
        $this->assign('id',$id);
        if(55 == $id){
             $this->display('status_31');
        }else{
            $this->display();
        }
        
    }
	public function add(){
        $user_id=get_user();
        $nowtime = time();
        $yestoday = $nowtime-24*60*60;
        $device_id=$_POST['id'];
        
        //判断是否需要开启新一轮的服务
        is_new_scan($device_id);

        $status = $_POST['status'];
        $shop_id = $_POST['shop_id'];//shop_id
        $onesta = array();//一级状态值id
        $twosta = array();//二级状态值id
        $onestep = 0;$twostep = 0;$is_normal=1;
        foreach ($status as $key => $value) {
            if ($key == 2) {
                $is_normal = 0;
            }
            //如果$value是数组，说明还有下一级状态
            if (is_array($value)) {
                $onesta[$onestep] = $key;//将状态id记录下来
                foreach ($value as $twokey => $twovalue) {
                    $twosta[$twostep]=$twokey;
                    $twostep=$twostep+1;
                    if(55 == $twokey){
                        if(!empty($twovalue) && $twovalue != 1){
                            $photo = M('unnormal_photo')->where(array('device_id'=>$device_id,'add_time'=>array('gt',$yestoday),'add_time'=>array('neq',$nowtime)))->find();

                            $thrData = array(
                                'photo_id'  => $twovalue,
                                'device_id' => $device_id,
                                'status_id' => 55,
                                'add_time'  => $nowtime
                            );
                            if ($photo) {
                                M('unnormal_photo')->where('id='.$photo['id'])->save($thrData);
                            }else{
                                M('unnormal_photo')->add($thrData);
                            }
                        }
                    }
                    //$twovalue如果是数组的话 是一个带有图片地址的数组
                    if (is_array($twovalue)) {
                        $maintain = array();
                        $maintain['user_id']=$user_id;
                        $maintain['device_id']=$device_id;
                        if($twokey == 4){
                            $maintain['reason']="破损";
                        }
                        if($twokey == 5){
                            $maintain['reason']="丢失";
                        }
                        
                        if(56 == $twokey || 59 == $twokey){
                            if(!empty($twovalue))
                            {
                                foreach ($twovalue as $tv_k => $tv_v) {
                                    $photo = M('unnormal_photo')->where(array('device_id'=>$device_id,'add_time'=>array('gt',$yestoday)))->find();

                                    $thrData = array(
                                        'photo_id'  => $tv_v,
                                        'device_id' => $device_id,
                                        'status_id' => $tv_k,
                                        'add_time'  => $nowtime
                                    );
                                    if ($photo) {
                                        M('unnormal_photo')->where('id='.$photo['id'])->save($thrData);
                                    }else{
                                        M('unnormal_photo')->add($thrData);
                                    }
                                }
                            }
                        }
                        foreach ($twovalue as $threekey => $threevalue) {
                            //如果id值是9，则是需要维修的
                            if($threekey == 9 || 20 == $threekey){
                                $maintain['status']=0;
                                $maintain['detail']="待公司确认赔偿或者客户购买新灯后更换新灯";
                                $maintain['img']=$threevalue;
                                $maint = M('maintain')->where(array('device_id'=>$device_id,'add_time'=>array('gt',$yestoday)))->find();
                                $maintain['add_time'] = $nowtime;
                                if ($maint) {
                                    M('maintain')->where('id='.$maint['id'])->save($maintain);
                                }else{
                                    M('maintain')->add($maintain);
                                }
                            }
                            //如果id是10，则是维修的，需要跟新维修记录
                            if($threekey == 10 || 21 == $threekey){
                                $maint = M('maintain')->where(array('device_id'=>$device_id,'status'=>0))->find();
                                $maintain['status']=1;
                                $maintain['detail']="客户已赔偿/购买新灯，此次服务更换新灯";
                                $maintain['img']=$threevalue;
                                $maintain['add_time'] = $nowtime;
                                if ($maint) {
                                    M('maintain')->where(array('device_id'=>$device_id,'status'=>0))->save($maintain);
                                }else{
                                    M('maintain')->add($maintain);
                                }
                            }
                        }
                    }
                }
            }else{
                //如果不是数组，说明没有下一级状态了
                $onesta[$onestep]=$key;//将状态id记录下来
            }
            $onestep=$onestep+1;
        }
        if (count($onesta)>0) {
            $onedata['device_id'] = $device_id;
            $onedata['status_id'] = implode(',', $onesta);
            $onedata['user_id'] = $user_id;
            $onedata['shop_id'] = $shop_id;
            $onedata['is_normal']=$is_normal;
            $onedata['add_time']=$nowtime;
            // 如果二级状态没有值，则保存0
            $onedata['twostatus_id']=empty($twosta) ? 0 : implode(',', $twosta);

            $oneres = M('status_log')->where(array('device_id'=>$device_id,'add_time'=>array('gt',$yestoday)))->find();
            if ($oneres) {
                M('status_log')->where('id='.$oneres['id'])->save($onedata);
            }else{
                M('status_log')->add($onedata);
            }
        }
        $feichong = array(
            'device_id' => $device_id,
            'shop_id'   => $shop_id,
            'add_time'  => $nowtime,
            'status'    => $is_normal,
            'user_id'   => $user_id
        );
        //整理animal数组
        $animal_temp = $_POST['animal'];
        $animals = array();
        $step=0;
        foreach ($animal_temp as $key => $value) {
            if($key == 2){
                $feichong['feie'] = $value;
            }elseif($key == 3){
                $feichong['cangy'] = $value;
            }elseif ($key == 4) {
                $feichong['wenzi'] = $value;
            }else{
                $feichong['other'] = $value;
            }
            $animals[$step] = array(
                'device_id' => $device_id,
                'number'    => $value,
                'animal_id' => $key,
                'add_time'  => $nowtime,
                'shop_id'   => $shop_id,
                'date_m'    => intval(date('m')),
                'date_y'    => intval(date('Y')),
            );
            $step++;
        }
        
        //判断这一期的扫描有没有进行，如果已经进行了，那么将对上一次添加做修改
        //判断条件是一天之内是否扫描过
        
        $is_scan_where=array(
            'device_id'=>$device_id,
            'add_time'=>array('gt',$yestoday),
        );
        //查找扫描记录表
        $is_scan=M('scan_log')->where($is_scan_where)->find();
        if ($is_scan) {
            $is_scan_where=array(
                'device_id'=>$device_id,
                'add_time'=>$is_scan['add_time'],
            );
            M('scan_log')->where('id='.$is_scan['id'])->save(array('add_time'=>$nowtime));
            M('feichong_log')->where($is_scan_where)->delete();
            M('feichong')->where($is_scan_where)->delete();
        }else{
            $scan_data = array(
                'user_id' => $user_id,
                'add_time'=> $nowtime,
                'device_id' => $device_id,
            );
            M('scan_log')->add($scan_data);
        }
        M('feichong')->add($feichong);
        $res = M('feichong_log')->addAll($animals);
        if ($res) {
            return show(1,'添加成功');
        }else{
            return show(0,'添加失败');
        }
	}
    public function scan_log(){
        $user_id = get_user();
        $page_no = I('post.page') ? I('post.page') : 1;
        $list = M('feichong')->page($page_no,20)
                ->where('user_id='.$user_id.' AND add_time > 0')->order('id desc')->select();
        foreach ($list as $key => $value) {
            if($value['status'] == 1){
                $list[$key]['status'] = '正常';
            }else{
                $list[$key]['status'] = '非正常';
            }
        }
        if (IS_POST) {
            $this->ajaxReturn($list);
        }
        $this->assign('list',$list);
        $this->display();
    }
}